jsx3.require("jsx3.gui.Template");

jsx3.lang.Class.defineClass("ux.core.gui.Form", jsx3.gui.Template.Block, null,
    function(FORM, form) {

        form.init = function(strName) {
            this.jsxsuper(strName);
        }

        form.onSetChild = function(objChild) {
            return objChild.instanceOf("ux.core.gui.FormPanel");
        }

        form.setChild = function(objChild, intPersist, strSourceURL, strNS) {
            var ret = this.jsxsuper(objChild, intPersist, strSourceURL, strNS);
            var me = this;
            setTimeout(function() {
                me.scanAndAddChildren(objChild);
                me.getParent().recalcBox();
                me.getParent().repaint();
            },0);
            return ret;
        }

        form.adoptChild = function(objChild, bRepaint, bForce) {
            var ret = this.jsxsuper(objChild, bRepaint, bForce);
            var me = this;
            setTimeout(function() {
                me.scanAndAddChildren(objChild);
                me.getParent().recalcBox();
                me.getParent().repaint();
            },0);
            return ret;
        }

        form.onRemoveChild = function(objChild, intIndex) {
            var ret = this.jsxsuper(objChild, intIndex);
            var me = this;
            setTimeout(function() {
                me.rescanAndAddChildren();
                me.getParent().recalcBox();
                me.getParent().repaint();
            },0);
            return ret;
        }

        form.removeChildren = function(children) {
            var ret = this.jsxsuper(children);
            var me = this;
            setTimeout(function() {
                me.rescanAndAddChildren();
                me.getParent().recalcBox();
                me.getParent().repaint();
            },0);
            return ret;
        }

        form.addChildRow = function(objChild, prevChild) {
            var idx = this.formrows.indexOf(prevChild);
            if(idx) {
                this.formrows.add(objChild, idx + 1);
            }
            else {
                this.rescanAndAddChildren();
            }
        }

        form.rescanAndAddChildren = function() {
            try {
            this.formrows.clear();
            var children = this.getChildren();
            for(var i=0; i<children.length; i++) {
                this.scanAndAddChildren(children[i]);
            }
            } catch(ex) {
                ux.core.System.logException(ex);
            }
        }

        form.scanAndAddChildren = function(panel) {
            if(!this.formrows) {
                this.formrows = new jsx3.util.List(0);
            }
            this.formrows.addAll(panel.getChildren());
        }

        form.onSetParent = function(objParent) {
            var ret = this.jsxsuper(objParent);
            return ret;
        }

        form.getNextFormRow = function(formrow) {
            var nextrow = formrow;
            var idx = this.formrows.indexOf(formrow);
            if(idx != -1) {
                idx++;
                if(idx >= this.formrows.size()) {
                    idx = 0;
                }
                nextrow = this.formrows.get(idx);
            }
            return nextrow;
        }

        form.getPreviousFormRow = function(formrow) {
            var prevrow = formrow;
            var idx = this.formrows.indexOf(formrow);
            if(idx != -1) {
                idx--;
                if(idx < 0) {
                    idx = this.formrows.size() - 1;
                }
                prevrow = this.formrows.get(idx);
            }
            return prevrow;
        }

        form.getCDFAttributes = function() {
            return this.cdfattributes
        }

        FORM.templateXML = ux.core.System.getServer().loadInclude("jsxaddin://user!LiveCore/classes/ux/core/gui/templates/form.xml","formPanel","xml").toString();
//        jsx3.log(FORM.templateXML);
        jsx3.gui.Template.compile(FORM.templateXML,FORM.jsxclass);
    }
);